#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
const int N = 6010, M = N * 2;
int h[N], e[M], ne[M], idx;
int f[N][2], w[N];
int n;
bool has_f[N];
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u)
{
    f[u][1] = w[u];
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        dfs(j);
        f[u][0] += max(f[j][1], f[j][0]);
        f[u][1] += f[j][0];
    }
}
int main()
{
    memset(h, -1, sizeof h);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> w[i];
    for (int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        add(b, a);
        has_f[a] = true;
    }
    int root = 1;
    while (has_f[root] && root <= n) root++;
    dfs(root);
    cout << max(f[root][0], f[root][1]) << endl;
    return 0;
}